package net.onewfour.config;

import java.io.File;
import java.util.Properties;

import javax.sql.DataSource;

import net.onewfour.dao.DBSuper;
import net.onewfour.interceptor.BaseInterceptor;
import net.onewfour.plugin.druid.MyDruidPlugin;
import static net.onewfour.control.BaseControl.THEME_FOLDER;

import org.apache.log4j.Logger;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.CMSHandlers;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.RenderFactory;
import com.jfinal.render.ViewType;

/***
 * CMSFileter
 * 
 * @author Marc
 * @version 1.0
 */
public class CMSConfig extends JFinalConfig {

	private static Logger log = Logger.getLogger(CMSConfig.class);

	private static final String _CONFIG_ = "config.properties";
	
	private static final String _VIEWEXTENSION_ = ".beetl";
	
	private static boolean _DEBUG = false ; 

	public CMSConfig() {
		BeetlRenderFactory.viewExtension = _VIEWEXTENSION_;
		RenderFactory.setMainRenderFactory(new BeetlRenderFactory());
	}
	private Properties properties = null;

	public Properties getProperties() {
		log.info("PATH:" + PathKit.getWebRootPath());
		if (this.properties == null) {
			properties = loadPropertyFile(_CONFIG_);
		}
		return properties;
	}

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(_DEBUG);
		me.setViewType(ViewType.OTHER);
		me.setMainRenderFactory(new BeetlRenderFactory());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new com.jfinal.core.CMSHandlers());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new BaseInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		try {
			getProperties();
			DBSuper.FullTable = properties.getProperty("fulltable");
			THEME_FOLDER = properties.getProperty("theme");
			_DEBUG = properties.getProperty("debug") != null;
			DataSource dSource = DruidDataSourceFactory
					.createDataSource(properties);
			MyDruidPlugin dp = new MyDruidPlugin(dSource);
			me.add(dp);

			ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
			arp.setShowSql(true);
			arp.setDevMode(true);
			me.add(arp);
			
			me.add(new EhCachePlugin(PathKit.getWebRootPath() + File.separator + "WEB-INF"
					+ File.separator +"ehcache.xml"));

		} catch (Exception e) {
			log.error("加载插件失败",e);
			throw new RuntimeException(e);
		}

	}

	@Override
	public void configRoute(Routes me) {
	}

	/* (non-Javadoc)
	 * @see com.jfinal.config.JFinalConfig#afterJFinalStart()
	 */
	@Override
	public void afterJFinalStart() {
		//启动完成之后的actionMapping 
		CMSHandlers.load();
	}
	
	

}
